package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 功能：
 * 作者：4204001140
 * 日期：2024/4/13 19:29
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // 设置分类的其他参数
        category.setStatus(StatusConstant.DISABLE);     // 默认为禁用状态
//        category.setCreateTime(LocalDateTime.now());    // 创建时间
//        category.setUpdateTime(LocalDateTime.now());    // 更新时间
        // 通过上下文获取当前操作的用户
//        Long currentId = BaseContext.getCurrentId();
//        category.setCreateUser(currentId);              // 创建人
//        category.setUpdateUser(currentId);              // 更新人
        categoryMapper.save(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 删除分类的时候，需要判断该分类下商品和菜单是否都清空了
        Integer countDish = dishMapper.countByCategoryId(id);
        if (countDish > 0){
            // 说明该分类下还有菜品
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        Integer countSetmeal = setmealMapper.countByCategoryId(id);
        if (countSetmeal > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 删除分类
        categoryMapper.deleteById(id);
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> pages =  categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 更新分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateTime(LocalDateTime.now());
//        Long currentId = BaseContext.getCurrentId();
//        category.setUpdateUser(currentId);
        categoryMapper.updateById(category);
    }


    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    @Override
    public void stopOrStart(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.updateById(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }


}
